/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.expense.common.util;

import com.expense.common.constant.enums.ResultCode;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private String msg = "success";

    private int code = 0;

    private T data;

    public Result() {
        super();
    }

    public Result(T data) {
        super();
        this.data = data;
    }

    public Result(T data, String msg) {
        super();
        this.data = data;
        this.msg = msg;
    }

    public Result(Throwable e) {
        super();
        this.msg = e.getMessage();
        this.code = ResultCode.FAIL.getCode();
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {return "msg='" + msg + '\'' +", code=" + code + ", data=" + data ;}

    public static <T> Result<T> fail() {
        Result<T> ret = new Result<T>();
        ret.setCode(ResultCode.FAIL.getCode());
        ret.setMsg(ResultCode.FAIL.getDesc());
        return ret;
    }

    public static <T>  Result<T> fail(T data) {
        Result<T> ret = Result.fail();
        ret.setData(data);
        return ret;
    }

    public static <T>  Result<T> fail(String msg) {
        Result<T> ret = Result.fail();
        ret.setMsg(msg);
        return ret;
    }


    public static <T> Result<T> fail(Integer code,String msg) {
        Result<T> ret = new Result<T>();
        ret.setCode(code);
        ret.setMsg(msg);
        return ret;
    }

    public static <T> Result<T> success() {
        Result<T> ret = new Result<T>();
        ret.setCode(ResultCode.SUCCESS.getCode());
        ret.setMsg(ResultCode.SUCCESS.getDesc());
        return ret;
    }

    public static <T>  Result<T> success(String msg) {
        Result<T> ret = Result.success();
        ret.setMsg(msg);
        return ret;
    }

    public static <T>  Result<T> success(T msg, Boolean isData) {
        if(!isData) return success(msg+"");
        else return success(msg);
    }

    public static <T> Result<T> success(T data) {
        Result<T> ret = Result.success();
        ret.setData(data);
        return ret;
    }

    public static <T> Result<T>  http404(T data) {
        Result<T> ret = new Result<T>();
        ret.setCode(ResultCode.NOT_FOUND.getCode());
        ret.setMsg(ResultCode.NOT_FOUND.getDesc());
        ret.setData(data);
        return ret;
    }

    public static <T> Result<T> http403(T data) {
        Result<T> ret = new Result<T>();
        ret.setCode(ResultCode.ACCESS_ERROR.getCode());
        ret.setMsg(ResultCode.ACCESS_ERROR.getDesc());
        ret.setData(data);
        return ret;
    }
}
